import { Component, OnInit } from '@angular/core';
import {ServiceLogicService} from '../../service/service-logic.service';
import {ActivatedRoute} from '@angular/router';
import {LeaveRecord, BasicInformation} from '../../domain/admin';

@Component({
  selector: 'app-approval-leave',
  templateUrl: './approval-leave.component.html',
  styleUrls: ['./approval-leave.component.css']
})
export class ApprovalLeaveComponent implements OnInit {
  basicInfo: BasicInformation = {};
  pendings: LeaveRecord[];     // 待审批请假记录
  processed: LeaveRecord[];   // 已处理的请假记录
  cols: any[];
  leaveRecord: LeaveRecord = {};
  displayDialog: boolean;
  selectedLeaveRecord: LeaveRecord;
  constructor(private logicService: ServiceLogicService) { }
  onRowSelect(event) {
    this.leaveRecord = this.cloneLeaveRecord(event.data);
    this.displayDialog = true;
  }
  save() {
    const leaveRecords = [...this.pendings];

    leaveRecords[this.pendings.indexOf(this.selectedLeaveRecord)] = this.leaveRecord;
    this.leaveRecord.state = parseInt(this.leaveRecord.state, 0);
    switch (this.leaveRecord.state) {
      case 0:
        this.leaveRecord.des = '未通过';
        break;
      case 2:
        this.leaveRecord.des = '通过';
        break;
      case 1:
        this.leaveRecord.des = '审核中';
        break;
      default:
        break;
    }
    console.log(this.leaveRecord);
    if (this.leaveRecord.state === 2) {
      this.logicService.getBasicInformation(this.leaveRecord.admin_id).then(res => {
        this.basicInfo = res[0]; console.log(123456); console.log(this.basicInfo);
        switch (this.leaveRecord.type) {
          case 0:
            this.basicInfo.compassionate_leave = this.basicInfo.compassionate_leave - this.leaveRecord.days;
            console.log(1234);
            break;
          case 1:
            this.basicInfo.sick_leave = this.basicInfo.sick_leave - this.leaveRecord.days;
            break;
          case 2:
            this.basicInfo.marriage_leave = this.basicInfo.marriage_leave - this.leaveRecord.days;
            break;
          case 3:
            this.basicInfo.maternity_leave = this.basicInfo.maternity_leave - this.leaveRecord.days;
            break;
          case 4:
            this.basicInfo.annual_leave = this.basicInfo.annual_leave - this.leaveRecord.days;
            break;
          default:
            break;
        }
        console.log(1212);
        console.log(this.basicInfo);
        this.logicService.updateBasicInformation(this.basicInfo).then(response => {} );
      });
    }
    if (this.leaveRecord.state === 2 || this.leaveRecord.state === 0) {
      const index = this.pendings.indexOf(this.selectedLeaveRecord);
      console.log(index);
      console.log(this.pendings);
      this.pendings = this.pendings.filter((nmnm, i) => i !== index);
      // this.pendings = leaveRecords;
      const processedRecords = [...this.processed];
      processedRecords.push(this.leaveRecord);
      this.processed = processedRecords;
    }
    this.logicService.approvalLeaveDaysLessThanThree(this.leaveRecord).then(res => {this.leaveRecord = null;
      this.displayDialog = false; });
  }
  cloneLeaveRecord(c: LeaveRecord): LeaveRecord {
    const leaveRecord = {};
    for (const prop in c) {
      if (c.hasOwnProperty(prop)) {
        leaveRecord[prop] = c[prop];
      }
    }
    return leaveRecord;
  }
  ngOnInit() {
    this.logicService.getLeaveDaysLessThanThree(1).then(res => { this.pendings = res; console.log(this.pendings);
      for (let j = 0; j < this.pendings.length; j++) {
        switch (this.pendings[j].type) {
          case 0:
            this.pendings[j].type_name = '事假';
            break;
          case 1:
            this.pendings[j].type_name = '病假';
            break;
          case 2:
            this.pendings[j].type_name = '婚假';
            break;
          case 3:
            this.pendings[j].type_name = '产假';
            break;
          case 4:
            this.pendings[j].type_name = '年假';
            break;
          default:
            break;
        }
      }
    });
    this.logicService.getLeaveDaysLessThanThree(5).then(res => {this.processed = res; console.log(this.processed);
      for (let j = 0; j < this.processed.length; j++) {
        switch (this.processed[j].type) {
          case 0:
            this.processed[j].type_name = '事假';
            break;
          case 1:
            this.processed[j].type_name = '病假';
            break;
          case 2:
            this.processed[j].type_name = '婚假';
            break;
          case 3:
            this.processed[j].type_name = '产假';
            break;
          case 4:
            this.processed[j].type_name = '年假';
            break;
          default:
            break;
        }
      }
    });
    this.cols = [
      {field: 'admin_id', header: 'admin_id'},
      {field: 'satrt_time', header: 'satrt_time'},
      {field: 'end_time', header: 'end_time'},
      {field: 'days', header: 'days'},
      {field: 'reason', header: 'reason'},
      {field: 'type_name', header: 'type_name'},
      {field: 'des', header: 'des'},
    ];
  }
}
